/*
* @Author: anchen
* @Date:   2016-07-05 14:33:35
* @Last Modified by:   anchen
* @Last Modified time: 2016-07-08 14:53:01
*/


define(['views/list', 'backbone'], function (views, Backbone) {

  var Router = Backbone.Router.extend({
    lastView: null,
    routes: {
      '' : 'homePage',
      'search' : 'searchPage',
      'search/:query'  : 'searchTagPage',
    },

      searchforms: function () {

          //console.log('click')
          //app.navigate('item', true);
          return false;

      },

     homePage: function () {
      this.payBack();
      this.lastView = new views.searchView();
      this.lastView.render();
    },

    searchPage: function () {
       //console.log(query);
       this.payBack();
       this.lastView = new views.searchView();
       this.lastView.searchform();
    },

    searchTagPage: function (query) {
      console.log(query);

       this.payBack();
       this.lastView = new views.searchView();
       this.lastView.searchrender({action:"tag",search:query});
    },

    // Backbone does not undelegate events on its own especially when multiple
    // views loading into the same **el**
    payBack: function () {
      if (this.lastView !== null) {
        this.lastView.undelegateEvents();
      }


    }
  });

  return Router;
});
